package com.pengcheng.hnweb.util;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONPath;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class JsonUtil {

    @Autowired
    private Common common;
    public JSONArray get_jsonarray_by_path(String jsonstr, String path)
    {
        try {
            Object json_str= JSONPath.eval(jsonstr,path);
            return JSONArray.parseArray(json_str.toString());
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return null;
        }
    }
    public JSONObject get_jsonobject_by_path(String jsonstr, String path)
    {
        try {
            Object json_str= JSONPath.eval(jsonstr,path);
            return JSONObject.parseObject(json_str.toString());
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return null;
        }
    }
    public String get_string_by_path(String jsonstr, String path)
    {
        try {
            Object json_str= JSONPath.eval(jsonstr,path);
            return json_str.toString();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return null;
        }
    }
    public int get_int_by_path(String jsonstr, String path)
    {
        try {
            Object json_str= JSONPath.eval(jsonstr,path);
            return Integer.parseInt(json_str.toString());
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return -99;
        }
    }
    public double get_double_by_path(String jsonstr, String path)
    {
        try {
            Object json_str= JSONPath.eval(jsonstr,path);
            return Double.parseDouble(json_str.toString());
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return -99;
        }
    }
    public boolean get_boolean_by_path(String jsonstr, String path)
    {
        try {
            Object json_str= JSONPath.eval(jsonstr,path);
            return Boolean.parseBoolean(json_str.toString());
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return false;
        }
    }
    public JSONArray get_json_array(JSONObject jo, String path)
    {
        String[] ss=path.split(",");
        JSONObject root=jo;
        JSONArray res=null;
        for(int i=0;i<ss.length;i++)
        {
            try
            {
                if(i<ss.length-1)
                {
                    root=root.getJSONObject(ss[i]);
                }
                else
                {
                    res= root.getJSONArray(ss[i]);
                }
            }
            catch (Exception ex)
            {
                log.info("pcshop",ex.toString());
                return null;
            }
        }
        return res;
    }
    public JSONObject get_json_object(JSONObject jo, String path)
    {
        String[] ss=path.split(",");
        JSONObject root=jo;
        JSONObject res=null;
        for(int i=0;i<ss.length;i++)
        {
            try
            {
                if(i<ss.length-1)
                {
                    root=root.getJSONObject(ss[i]);
                }
                else
                {
                    res= root.getJSONObject(ss[i]);
                }
            }
            catch (Exception ex)
            {
                log.info("pcshop",ex.toString());
                return null;
            }
        }
        return res;
    }
    public boolean is_empty(String str)
    {
        if(str==null||str.equals(""))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public String get_json_string(JSONObject jo, String path)
    {
        String[] ss=path.split(",");
        JSONObject root=jo;
        String res=null;
        for(int i=0;i<ss.length;i++)
        {
            try
            {
                if(i<ss.length-1)
                {
                    root=root.getJSONObject(ss[i]);
                }
                else
                {
                    res= root.getString(ss[i]);
                }
            }
            catch (Exception ex)
            {
                log.info("pcshop",ex.toString());
                return null;
            }
        }
        return res;
    }

    public int get_json_int(JSONObject jo, String path)
    {
        String[] ss=path.split(",");
        JSONObject root=jo;
        int res=-99;
        for(int i=0;i<ss.length;i++)
        {
            try
            {
                if(i<ss.length-1)
                {
                    root=root.getJSONObject(ss[i]);
                }
                else
                {
                    res= root.getInteger(ss[i]);
                }
            }
            catch (Exception ex)
            {
                log.info("pcshop",ex.toString());
                return -99;
            }
        }
        return res;
    }
    public double get_json_double(JSONObject jo, String path)
    {
        String[] ss=path.split(",");
        JSONObject root=jo;
        double res=-99;
        for(int i=0;i<ss.length;i++)
        {
            try
            {
                if(i<ss.length-1)
                {
                    root=root.getJSONObject(ss[i]);
                }
                else
                {
                    res= root.getDouble(ss[i]);
                }
            }
            catch (Exception ex)
            {
                log.info("pcshop",ex.toString());
                return -99;
            }
        }
        return res;
    }
}
